package com.example.demoes.service.impl;

import com.example.demoes.entity.OmsOrder;
import com.example.demoes.mapper.OmsOrderMapper;
import com.example.demoes.repository.EsOrderRepository;
import com.example.demoes.service.OmsOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author rosszhang
 * @since 2023-12-09
 */
@Service
@RequiredArgsConstructor
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder> implements OmsOrderService {
    private final EsOrderRepository orderRepository;

    @Override
    public List<OmsOrder> getEsList() {
        // 从数据库中查询所有的订单
        List<OmsOrder> allOrders = this.baseMapper.queryList();
        // 调用Es Repository 操作，增删改查等
        orderRepository.saveAll(allOrders);
        return allOrders;
    }

    @Override
    public List<OmsOrder> getEs(String memberUsername) {
        return orderRepository.findByMemberUsername(memberUsername);
    }

    @Override
    public Page<OmsOrder> getEsQuery(String memberUsername) {
        OmsOrder order=new OmsOrder();
        order.setMemberUsername(memberUsername);
        return this.orderRepository.searchSimilar(order, new String[]{"memberUsername"}, PageRequest.of(0, 100));
    }

    @Override
    public void deletedEsById(Long id) {
        orderRepository.deleteById(id);
    }

    @Override
    public long countEs() {
        return orderRepository.count();
    }
}
